#!/bin/sh

case "$1" in
    help)
        echo simple wrapper over dinit for managing xdg autostart shit
        echo
        echo \`autostartctl boot\` to start the thing,
        echo \`autostartctl generate\` to generate services from ~/.config/autostart,
        echo and everything else is directly passed to \`dinitctl\`
        ;;
    boot)
        exec dinit -u -d ~/.var/autostart.d -p ~/.var/autostart.sock
        ;;
    generate)
        files="$(ls "$HOME/.config/autostart")"
        mkdir -p "$HOME/.var/autostart.d"

        for file in $files; do
            exec="$(grep '^Exec=' < "$HOME/.config/autostart/$file" | sed 's/Exec=//')"
            file_nodesktop="$(echo $file | sed 's/\.desktop$//g')"
            printf "type = process\ncommand = %s\nrestart = true\nstop-timeout = 0\n" "$exec" > "$HOME/.var/autostart.d/$file_nodesktop"
            autostartctl enable "$file_nodesktop"
        done
        ;;
    *)
        dinitctl -p "$HOME/.var/autostart.sock" $@
        ;;
esac

